package com.dengqi.mymodule.activity.fragment;

import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

/**
 * 坑一：
 * 重写isViewFromObject方法导致fargmnet空白
 *
 * FragmentStatePagerAdapter:
 * 切换：
 * onCreate-onStart-onResume
 *
 * FragmentPagerAdapter:
 * 切换：
 * onStart-onResume
 *
 * 2种都会destory
 * onPause-onStop-onDestroyView
 */
public class ViewPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> mViewList ;
    private List<String> mTitleList ;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        this.mViewList = new ArrayList<>();
        this.mTitleList = new ArrayList<>();
    }

    public void setTitle(List<String> mTitleList){
        this.mTitleList = mTitleList;
    }

    public void setData(List<Fragment> mViewList){
        this.mViewList = mViewList;
    }

    @Override
    public int getCount() {
        return mViewList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(mTitleList.size()>0){
            return mTitleList.get(position);
        }else{
            return super.getPageTitle(position);
        }
    }

    @Override
    public Fragment getItem(int position) {
        return mViewList.get(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }


}
